<?php
/**
 * Class Order
 * Desc  订单Model
 * Create on 2021/8/18 15:40
 * Create by wangyafang
 */

namespace app\api\model\xyb;


class Order extends Base
{
    protected $name = 'xyb_order';

// 追加属性
    protected $append = [
        'type_text',
        'express_type_text',
//        'down_method_text',
    ];

//    protected $auto = ['end'];
    protected $insert = ['price','total_fee'];

    public function getTypeList()
    {
        return array(
            '1' => '帮我送',
            '2' => '取快递',
            '3' => '帮我取',
            '4' => '代我办',
        );
    }

    public function getTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getTypeTextName($value, $data = [])
    {
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public static function getTypeNameTextName($value, $data = [])
    {
        $type_list =  array(
            '1' => '帮我送',
            '2' => '取快递',
            '3' => '帮我取',
            '4' => '代我办',
        );
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        return isset($type_list[$value]) ? $type_list[$value] : '';
    }

    public function getExpressTypeList()
    {
        return ['1' => '小件', '2' => '中件', '3' => '大件'];
    }

    public function getExpressTypeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['express_type']) ? $data['express_type'] : '');
        $list = $this->getExpressTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getDownMethodList()
    {
        return ['1' => '仅下载文档', '2' => '百度网盘下载',];
    }

    public function getDownMethodTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['down_method']) ? $data['down_method'] : '');
        $list = $this->getDownMethodList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function user()
    {
        return $this->belongsTo('app\common\model\User', 'user_id', 'id')->bind('nickname,avatar');
    }

    public function setDestinationAttr($value)
    {

//        $userAddressObj = (new UserAddress())->get($value);
//        $value = $userAddressObj['group_name'].'-'.$userAddressObj['detail'];
        return $value;
    }

    public function getStartAttr($value,$data)
    {
//        if($data['type'] == 2) {
//            return (new Express())->where('id',$value)->value('name');
//        }
        return $value;
    }

    public function getExpressAttr($value)
    {

        if(!empty($value)) {
            $json_array = json_decode($value,true);
            $tem = '';
            foreach ($json_array as $kk=>$vv) {
                $tem = $tem.(isset($vv['type'])?$vv['type']:$vv['name']).':'.$vv['number'].',';
            }
            return substr($tem, 0, -1);
        }
        return '';
    }

    public function setPriceAttr($value,$data)
    {
        if($data['type'] == 2) {
            $tem = 0;
            $express_array = json_decode($data['express'],true);
            foreach ($express_array as $k=>$v) {
                $tem = bcadd($tem,bcmul($v['price'],$v['number'],2),2);
            }
            return $tem;
        }
        return $value;
    }
}